package com.eorchis.module.trainingquestionnaire.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.module.trainingneeds.domain.QuestionBank;
import com.eorchis.module.trainingneeds.querybean.ItemOption;
import com.eorchis.module.trainingneeds.querybean.ObjectiveItems;
import com.eorchis.module.trainingneeds.querybean.ProblemInfo;
import com.eorchis.module.trainingneeds.querybean.UserInfoBean;
import com.eorchis.module.trainingquestionnaire.dao.IUserAnswerLinkDao;
import com.eorchis.module.trainingquestionnaire.domain.MCQStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.QuestionsStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.RadioStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.ScoreStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.StatisticsHeaderBean;
import com.eorchis.module.trainingquestionnaire.domain.UserAnswerLink;
import com.eorchis.module.trainingquestionnaire.domain.UserAnswerLinkQueryBean;
import com.eorchis.module.trainingquestionnaire.service.IUserAnswerLinkService;
import com.eorchis.module.trainingquestionnaire.ui.commond.UserAnswerLinkQueryCommond;
import com.eorchis.module.trainingquestionnaire.ui.commond.UserAnswerLinkValidCommond;
import com.eorchis.utils.BeanUtils;

/**
 * Title: UserAnswerLinkServiceImpl<br>
 * Description: 用户答卷关联信息管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-08-28
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.trainingquestionnaire.service.impl.UserAnswerLinkServiceImpl")
public class UserAnswerLinkServiceImpl 
			extends AbstractBaseService 
		implements IUserAnswerLinkService {
		
	@Autowired
	@Qualifier("com.eorchis.module.trainingquestionnaire.dao.impl.UserAnswerLinkDaoImpl")
	private IUserAnswerLinkDao userAnswerLinkDao;
		

	public IDaoSupport getDaoSupport() {
		return userAnswerLinkDao;
	}
	@SuppressWarnings("unchecked")
	public UserAnswerLinkValidCommond toCommond(IBaseEntity entity) {
		return new UserAnswerLinkValidCommond((UserAnswerLink)entity);
	}
	
	
	/**
	 * 统计某一个问卷下所有的主观题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-28
	 */
	@Override
	public List<UserAnswerLinkQueryBean> findAllSubjectivesByQnId(UserAnswerLinkQueryCommond qCommond) throws Exception {
		List<UserAnswerLinkQueryBean> list=userAnswerLinkDao.findAllSubjectivesByQnId(qCommond);
		if(list!=null&&list.size()>0) {
			for(int i=0;i<list.size();i++) {
				UserAnswerLinkQueryBean ualq=list.get(i);
				ualq.setQuestionOrderName("第"+(i+1)+"题");
			}
		}
		return list;
	}
	
	
	/**
	 * 统计---根据试题id查询一道主观题的的所以的回答情况
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-28
	 */
	@Override
	public List<UserAnswerLinkQueryBean> findAllSubjectivesAnByQId(UserAnswerLinkQueryCommond qCommond) throws Exception {
		return userAnswerLinkDao.findAllSubjectivesAnByQId(qCommond);
	}
	
	/**
	 * 统计---某一个问卷下所有的客观题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-01
	 */
	@Override
	public ObjectiveItems findAllObjectiveByQnId(UserAnswerLinkQueryCommond qCommond) throws Exception {
		//查询所有的客观题
		List<UserAnswerLinkQueryBean> questionList=userAnswerLinkDao.findAllObjectiveByQnId(qCommond);
		//查询所有的客观题中的选项
		List<UserAnswerLinkQueryBean> optionList=userAnswerLinkDao.findOptionSubmitNum(qCommond);
		
		//对选项集合optionList封装成map begin
		Map<String,List<ItemOption>> optionMap=new LinkedHashMap<String,List<ItemOption>>();
		if(optionList!=null&&optionList.size()>0) {
			for(int i=0;i<optionList.size();i++) {
				UserAnswerLinkQueryBean ulqb=optionList.get(i);
				
				ItemOption io=new ItemOption();
				BeanUtils.copyProperties(ulqb,io);
				
				if(optionMap.get(ulqb.getItemId())!=null) {
					List<ItemOption> oplist=optionMap.get(ulqb.getItemId());
					oplist.add(io);
					optionMap.put(ulqb.getItemId(), oplist);
				}else {
					List<ItemOption> oplist=new ArrayList<ItemOption>();
					oplist.add(io);
					optionMap.put(ulqb.getItemId(), oplist);
				}
			}
		}
		//对选项集合optionList封装成map end
		
		//单选
		List<ProblemInfo> selectProblemInfo=new ArrayList<ProblemInfo>();
		//多选
		List<ProblemInfo> multipleSelectProblemInfo=new ArrayList<ProblemInfo>();
		
		//试题和选项集合合并
		if(questionList!=null&&questionList.size()>0) {
			for(int i=0;i<questionList.size();i++) {
				UserAnswerLinkQueryBean ualq=questionList.get(i);
				ProblemInfo pi=new ProblemInfo();
				BeanUtils.copyProperties(ualq,pi);
				
				//计算%分比
				
				List<ItemOption> itps=optionMap.get(pi.getItemId());
				DecimalFormat df = new DecimalFormat("###.0"); 
				if(itps!=null&&itps.size()>0) {
					for(int z=0;z<itps.size();z++) {
						ItemOption in=itps.get(z);
						if(!ualq.getItemTotalNum().equals(0)) {
							double selectnum=in.getSelectNum();
							double allnum=ualq.getItemTotalNum();
							if(!in.getSelectNum().equals(0)) {
							double shu=selectnum/allnum;
							  in.setProportion((df.format(shu*100))+"%");
							}else {
							  in.setProportion("0.0%");
							}
						}else {
							in.setProportion("0%");
						}
					}
				}
				pi.setItemOption(itps);
				//计算%分比
				
				if(ualq.getItemType().equals(QuestionBank.QUESTION_DAN)) {
					selectProblemInfo.add(pi);
				}else if(ualq.getItemType().equals(QuestionBank.QUESTION_DUO)){
					multipleSelectProblemInfo.add(pi);
				}
			}
		}
		ObjectiveItems ois=new ObjectiveItems();
	    ois.setSelectProblemInfo(selectProblemInfo);
	    ois.setMultipleSelectProblemInfo(multipleSelectProblemInfo);
		return ois;
	}
	
	
	/**
	 * 统计---查询试卷提交人员信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-01
	 */
	@Override
	public List<UserInfoBean> getUserListByPnId(UserAnswerLinkQueryCommond qCommond) throws Exception {
		return userAnswerLinkDao.getUserListByPnId(qCommond);
	}
	
	/**
	 * 统计---查询某个试卷中的所有试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-04
	 */
	@Override
	public ObjectiveItems findAllQuestionByQnId(UserAnswerLinkQueryCommond qCommond) throws Exception{
		//试题选项答案集合
		List<UserAnswerLinkQueryBean> allQuestion=userAnswerLinkDao.findAllQuestionByQnId(qCommond);
		//问答题
		List<ProblemInfo> questionsAnswersInfo=new ArrayList<ProblemInfo>();
		
		if(allQuestion!=null&&allQuestion.size()>0) {
			
			//试题提干
			List<UserAnswerLinkQueryBean> selectList=new ArrayList<UserAnswerLinkQueryBean>();
			List<UserAnswerLinkQueryBean> multipleList=new ArrayList<UserAnswerLinkQueryBean>();
			
			//试题选项
			Map<String,List<ItemOption>> selectmap=new TreeMap<String,List<ItemOption>>();
			Map<String,List<ItemOption>> multiplemap=new TreeMap<String,List<ItemOption>>();
			
			//试题选项
			for(int i=0;i<allQuestion.size();i++) {
				UserAnswerLinkQueryBean ulqb=allQuestion.get(i);
				//试题选项
				ItemOption it=new ItemOption();
				it.setOptionNum(ulqb.getOptionNum());
				it.setOptionName(ulqb.getOptionName());
				if(ulqb.getItemAnswerNum()!=null&&ulqb.getItemAnswerNum().equals(ulqb.getOptionNum())) {
					it.setSelect(true);
				}
				if(ulqb.getItemType()!=null&&ulqb.getItemType().equals(QuestionBank.QUESTION_DAN)) {
					if(selectmap.get(ulqb.getItemId())!=null) {
						List<ItemOption> list=selectmap.get(ulqb.getItemId());
						list.add(it);
						selectmap.put(ulqb.getItemId(), list);
					}else {
						List<ItemOption> list=new ArrayList<ItemOption>();
						list.add(it);
						selectmap.put(ulqb.getItemId(), list);
						selectList.add(ulqb);
					}
				}else if(ulqb.getItemType()!=null&&ulqb.getItemType().equals(QuestionBank.QUESTION_DUO)){
					if(multiplemap.get(ulqb.getItemId())!=null) {
						List<ItemOption> list=multiplemap.get(ulqb.getItemId());
						list.add(it);
						multiplemap.put(ulqb.getItemId(), list);
					}else {
						List<ItemOption> list=new ArrayList<ItemOption>();
						list.add(it);
						multiplemap.put(ulqb.getItemId(), list);
						multipleList.add(ulqb);
					}
				}else if(ulqb.getItemType()!=null&&ulqb.getItemType().equals(QuestionBank.QUESTION_WEN)){
					ProblemInfo pi=new ProblemInfo();
					BeanUtils.copyProperties(ulqb,pi);
					pi.setOption(ulqb.getItemAnswerStr());
					questionsAnswersInfo.add(pi);
				}
			}
			
			
			//单选
			List<ProblemInfo> selectProblemInfo=new ArrayList<ProblemInfo>();
			//多选
			List<ProblemInfo> multipleSelectProblemInfo=new ArrayList<ProblemInfo>();
			
			for(UserAnswerLinkQueryBean bean:selectList) {
				//System.out.print(entry.getKey() + ":" + entry.getValue() + "\t");
				ProblemInfo pi=new ProblemInfo();
				BeanUtils.copyProperties(bean,pi);
				pi.setItemOption(selectmap.get(bean.getItemId()));
				selectProblemInfo.add(pi);
			}
			
			for(UserAnswerLinkQueryBean bean:multipleList) {
				ProblemInfo pi=new ProblemInfo();
				BeanUtils.copyProperties(bean,pi);
				pi.setItemOption(multiplemap.get(bean.getItemId()));
				multipleSelectProblemInfo.add(pi);
			}
			
			
			ObjectiveItems ois=new ObjectiveItems();
		    ois.setSelectProblemInfo(selectProblemInfo);
		    ois.setMultipleSelectProblemInfo(multipleSelectProblemInfo);
		    ois.setQuestionsAnswersInfo(questionsAnswersInfo);
		    return ois;
		}
		return null;
	}
	
	/**
	 * 用户提交试卷，答案信息批量入库
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-09
	 */
	@Override
	public boolean addUserAnswerLink(UserAnswerLinkQueryCommond qCommond) throws Exception{
		return userAnswerLinkDao.addUserAnswerLink(qCommond);
	}
	
	/**
	 * 暂存试卷答案信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-05
	 */
	@Override
	public boolean addUserAnswerLinkTemp(UserAnswerLinkQueryCommond queryCommond) throws Exception
	{
		//清除上次暂存的答案信息
		this.delUserAnswerLinkTemp(queryCommond);
		
		return userAnswerLinkDao.addUserAnswerLinkTemp(queryCommond);
	}
	/**
	 * 删除暂存的试卷答案信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-05
	 */
	@Override
	public boolean delUserAnswerLinkTemp(UserAnswerLinkQueryCommond queryCommond) throws Exception
	{
		return userAnswerLinkDao.delUserAnswerLinkTemp(queryCommond);
	}
	/**
	 * 查询暂存的试卷答案信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-05
	 */
	@Override
	public Map<String, String> queryUserAnswerLinkTemp(UserAnswerLinkQueryCommond queryCommond) throws Exception
	{
		return userAnswerLinkDao.queryUserAnswerLinkTemp(queryCommond);
	}
	/**
	 * 查询用户答卷信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-05
	 */
	@Override
	public Map<String, List<String>> queryUserAnswer(UserAnswerLinkQueryCommond queryCommond) throws Exception
	{
		return userAnswerLinkDao.queryUserAnswer(queryCommond);
	}

	/**
	 * 查询统计表头信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-3-1
	 */
	@Override
	public StatisticsHeaderBean findHeader(UserAnswerLinkQueryCommond queryCommond) throws Exception {
		return userAnswerLinkDao.findHeader(queryCommond);
	}
	/**
	 * 查询数值型单选题统计列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-24
	 */
	@Override
	public List<ScoreStatisticsBean> findScoreRadioList(UserAnswerLinkQueryCommond queryCommond) throws Exception {
		return userAnswerLinkDao.findScoreRadioList(queryCommond);
	}
	/**
	 * 查询文字型单选题统计列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-25
	 */
	@Override
	public List<RadioStatisticsBean> findTextRadioList(UserAnswerLinkQueryCommond queryCommond) throws Exception {
		return userAnswerLinkDao.findTextRadioList(queryCommond);
	}
	/**
	 * 查询多选题统计列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-25
	 */
	public List<MCQStatisticsBean> findMCQList(UserAnswerLinkQueryCommond queryCommond) throws Exception {
		return userAnswerLinkDao.findMCQList(queryCommond);
	}
	/**
	 * 查询问答题统计列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-26
	 */
	@Override
	public List<QuestionsStatisticsBean> findQuestionsList(UserAnswerLinkQueryCommond queryCommond) throws Exception {
		return userAnswerLinkDao.findQuestionsList(queryCommond);
	}
	/**
	 * 全部用户答卷信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2016-2-24
	 */
	@Override
	public Map<String, Map<String, List<String>>> queryAllUserAnswer(UserAnswerLinkQueryCommond queryCommond) throws Exception
	{
		return userAnswerLinkDao.queryAllUserAnswer(queryCommond);
	}
}
